package com.duanjc.shiro.controller;

import com.duanjc.shiro.entity.TUser;
import com.duanjc.shiro.service.TUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * [一句话描述该类的功能]
 *
 * @author : [duanjiangcheng]
 * @version : [v1.0]
 * @createTime : [2022/11/29 15:59]
 */
@Slf4j
@Controller
public class IndexController {

    @Autowired
    private TUserService tUserService;

    @GetMapping("/")
    public String home(Model model) {
        String msg = "Welcome to Thymeleaf Template";
        // adding the attribute(key-value pair)
        model.addAttribute("message", msg);
        // returning the view name
        return "index";
    }

    @GetMapping("/index")
    public String index(Model model) {
        String msg = "Welcome to Thymeleaf Template";
        // adding the attribute(key-value pair)
        model.addAttribute("message", msg);
        // returning the view name
        log.info("重定向到index页面");

        tUserService.all().forEach(i -> log.info(i.toString()));
        return "index";
    }

    @GetMapping("/loginPage")
    public String loginPage(Model model) {
        String msg = "Welcome to Thymeleaf Template";
        // adding the attribute(key-value pair)
        model.addAttribute("message", msg);
        // returning the view name
        return "login";
    }

    @GetMapping("/registerPage")
    public String registerPage(Model model) {
        String msg = "Welcome to Thymeleaf Template";
        // adding the attribute(key-value pair)
        model.addAttribute("message", msg);
        // returning the view name
        return "register";
    }

    @RequestMapping("register")
    public String register(TUser user) {
        try {
            //tUserService.register(user);
            return "redirect:/login.jsp";
        } catch (Exception e) {

            e.printStackTrace();
            return "redirect:/register.jsp";
        }
    }

    @RequestMapping(method = RequestMethod.POST, path = "/login")
    public String login(String username, String password, HttpSession session, @RequestParam(defaultValue = "false") boolean rememberMe, HttpServletResponse response) {
        try {
            //获取主体对象
            Subject subject = SecurityUtils.getSubject();
            //认证
            AuthenticationToken token = new UsernamePasswordToken(username, password,rememberMe);
            subject.login(token);
            session.setAttribute("user", token.getPrincipal().toString());

            if (rememberMe) {
                /*var username = getCookie("cookie_username") || "admin";
                var password = getCookie("cookie_password");
                var remember = getCookie("cookie_remember");*/
                Cookie cookie = new Cookie("cookie_username",username);
                Cookie cookie2 = new Cookie("cookie_password",password);
                Cookie cookie3 = new Cookie("cookie_remember","1");
                cookie.setMaxAge(24 * 60 * 60);
                cookie2.setMaxAge(24 * 60 * 60);
                cookie3.setMaxAge(24 * 60 * 60);
                response.addCookie(cookie);
                response.addCookie(cookie2);
                response.addCookie(cookie3);
            }

            return "redirect:/index";
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("用户名错误");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("密码错误");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return "redirect:/login.html";
    }

    /** * 退出登录 * * @return */
    @RequestMapping("logout")
    public String logout() {

        Subject subject = SecurityUtils.getSubject();
        subject.logout();//退出用户
        return "login";
    }

    @RequiresPermissions("tengxun")
    @GetMapping("/add")
    public void add() {
       log.info("用户点击了add");
    }
}
